import request from '@/config/axios'
import type { paramsType, resultType } from './types'
import axios from 'axios'
import { useAppStore } from '@/store/modules/app'
import { config } from '@/config/axios/config'

interface IUMResponse<T = any> {
  code: number
  message: string
  result: T extends any ? T : T & any
  success: boolean
  timestamp: number
}

// 查询用户列表
export const userListApi = (params: paramsType): Promise<IUMResponse<resultType>> => {
  return request.get({ url: '/sys/user/list', params })
}
// 删除用户
export const deleteUserApi = (ids: string): Promise<IUMResponse> => {
  return request.post({ url: `/sys/user/deleteBatch?ids=${ids}` })
}
// 添加用户
export const addUserApi = (data): Promise<IUMResponse> => {
  return request.post({ url: '/sys/user/add', data })
}
// 编辑用户
export const editUserApi = (data): Promise<IUMResponse> => {
  return request.post({ url: '/sys/user/edit', data })
}
// 修改密码
export const changePasswordApi = (userId: string, password: string): Promise<IUMResponse> => {
  return request.post({
    url: `/sys/user/changePassword?userId=${userId}&password=${password}`
  })
}
// 冻结、解冻用户
export const frozenUserApi = (ids: string, status: string | number): Promise<IUMResponse> => {
  return request.post({ url: `/sys/user/frozenBatch?ids=${ids}&status=${status}` })
}
// 根据用户id获取关联角色信息
export const queryUserRoleApi = (params: any): Promise<IUMResponse> => {
  return request.get({ url: '/sys/user/queryUserRole', params })
}
// 根据用户id获取关联部门信息
export const userDepartListApi = (params: any): Promise<IUMResponse> => {
  return request.get({ url: '/sys/user/userDepartList', params })
}
// 导出功能
export const exportApi = (params: any): Promise<IUMResponse> => {
  return request.get({ url: '/sys/user/export', params })
}
// 上传头像
export const uploadAvatarApi = (file: File): Promise<any> => {
  const appStore = useAppStore()
  const formData = new FormData()
  formData.append('file', file)
  return axios({
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data',
      'x-access-token': appStore.token
    },
    url: `${config.base_url.base}/sys/user/uploadFile`,
    data: formData
  })
}
export const uploadExcelApi = (file: File): Promise<any> => {
  const appStore = useAppStore()
  const formData = new FormData()
  formData.append('file', file)
  return axios({
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data',
      'x-access-token': appStore.token
    },
    url: `${config.base_url.base}/sys/user/importExcel`,
    data: formData
  })
}
